//
//  HoPageBaseMenu.swift
//  
//
//  Created by Hoa on 2021/3/31.
//  Copyright © 2020 Hoa. All rights reserved.
//

import UIKit
import Bond
import ReactiveKit

open class HoPageBaseMenu: UIView {
    
    private var dataCenter: HoPageDataCenter!
    private let disposeBag = DisposeBag()
    
    public init(dataCenter: HoPageDataCenter) {
        super.init(frame: .zero)
        self.dataCenter = dataCenter
        dataCenter.selectedIndex.observeNext { [weak self]index in
            self?.setIndex(index: index)
        }.dispose(in: disposeBag)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    /**
     * 被动设置 由外部触发
     * 设置选中下标
     */
    open func setIndex(index: Int) {
        
    }
    
    /**
     * 初始化设置
     * 设置所有菜单下标
     */
    public func setTags(_ items: [UIView]) {
        items.enumerated().forEach { (offset, item) in
            item.tag = offset
        }
    }
    
    /**
     * 菜单点击某个选项设置
     * 主动点击
     */
    @objc public func itemClick(_ sender: UIView) {
        dataCenter.selectedIndex.value = sender.tag
    }
}
